﻿/*
*		2016年12月8日定義頭文件
*		作者:曹國鴻
*		詳見註釋
*		伤心了,ttf的还要自己编译....就酱吧,有时间了在完善,優化,寫的還是太匆忙了,好亂好亂....不是故意噠(>﹏<)
*		項目所在鏈接網址:http://git.oschina.net/doudoutu/Cplus-plus-of-end	(土豆豆的項目)
*/
#pragma once
#include <iostream>
#include <SDL.H>
#include "SDL_image.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
# include <stdlib.h>
# include <sys/types.h>
# include <sys/timeb.h>
#include <windows.h>

#ifndef SCREEN_WIDTH
#undef main											//不加這個總是莫名其妙的報錯
#define SCREEN_WIDTH 800			//定義窗口的寬度
#define SCREEN_HEIGHT 480		//定義窗口的高度


SDL_Window * g_pWindow = NULL;//窗口
SDL_Renderer * g_pRenderer = NULL;//渲染器
SDL_Texture * g_pTexture = NULL;//图层纹理
SDL_Texture * g_pHammer = NULL;//圖層錘子
SDL_Texture * g_pOnMouse = NULL;//出現倉鼠
SDL_Rect h;//錘子大小及位置定義
SDL_Rect m;//倉鼠的位置及大小

#endif // !SCREEN_WIDTH
int qw = 0;//判斷倉鼠是否存在,存在不爲0,不存在爲0
int weizhi[10] = { 0,0,0,0,0,0,0,0,0,0 };//位置判断
int so=0;//初始化分數
//完成初始化操作函數
int init();

//完成釋放函數
void close();

//加載圖片函數
int load(const char * szPath);

//加載倉鼠位置
int mouse();

//加載錘子
int hammer_show(int a, int m_x, int m_y);

//出現倉鼠
int on_mouse(int num);

//分析倉鼠是否存在
void Analyzing_presence_hamster();

//打擊倉鼠
void hit_mouse(int x, int y);

//初始化爲0失敗,爲1成功
int init() {
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		printf("SDL初始化失敗!%s\n", SDL_GetError());
		return 0;
	}

	g_pWindow = SDL_CreateWindow("name", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
	if (NULL == g_pWindow) {
		printf("窗口創建失敗!%s\n", SDL_GetError());
		return 0;
	}

	g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, SDL_RENDERER_TARGETTEXTURE);
	if (g_pRenderer == NULL) {
		printf("渲染器創建失敗!%s\n", SDL_GetError());
		return 0;
	}

	return 1;
}

void close(void) {
	if (g_pTexture != NULL) {
		SDL_DestroyTexture(g_pTexture);
		g_pTexture = NULL;
	}
	if (g_pRenderer != NULL) {
		SDL_DestroyRenderer(g_pRenderer);
		g_pRenderer = NULL;
	}
	if (g_pWindow != NULL) {
		SDL_DestroyWindow(g_pWindow);
		g_pWindow = NULL;
	}
	SDL_Quit();
	printf("程序释放成功\n");
}

//加载图片函数,1成功,0失败
int load(const char * szPath) {
	g_pTexture = IMG_LoadTexture(g_pRenderer, szPath);
	if (NULL == g_pTexture) {
		printf("加载  %s  图片失败!\n", szPath);
		return 0;
	}
	return 1;
}

//a爲0時,錘子擡起,a爲1時,錘子落下
int hammer_show(int a, int m_x, int m_y)
{
	SDL_RenderCopy(g_pRenderer, g_pTexture, NULL, NULL);
	if (a == 0)
	{
		g_pHammer = IMG_LoadTexture(g_pRenderer, "../img/hammer.png");
	}
	else
	{
		g_pHammer = IMG_LoadTexture(g_pRenderer, "../img/hammer_down.png");
	}
	if (g_pHammer == NULL)
	{
		printf("貌似錘子沒有加載成功哎╮(￣▽￣\")╭,即將退出\n,%s", SDL_GetError());
		return 0;
	}
	h.x = m_x - 50;//x軸座標
	h.y = m_y - 60;
	if (a == 0)
	{
		h.w = 120;
		h.h = 100;
	}
	else
	{
		h.w = 120;
		h.h = 60;
	}
	SDL_RenderCopy(g_pRenderer, g_pHammer, NULL, &h);
	SDL_RenderPresent(g_pRenderer);
	return 1;
}

int mouse() {
	int temp;
	struct timeb stb;
	ftime(&stb);
	srand((unsigned)stb.millitm);
	temp = rand() % 9;
	weizhi[temp] = 1;
	return 0;
}

int on_mouse(int num) {
	g_pOnMouse = IMG_LoadTexture(g_pRenderer, "../img/mouse.png");
	m.h = 68;m.w = 53;
	switch (num)
	{
	case 0:
		m.x = 126;m.y = 313;
		break;
	case 1:
		m.x = 352;m.y = 320;
		break;
	case 2:
		m.x = 583;m.y = 320;
		break;
	case 3:
		m.x = 133;m.y = 219;
		break;
	case 4:
		m.x = 355;m.y = 219;
		break;
	case 5:
		m.x = 559;m.y = 217;
		break;
	case 6:
		m.x = 167;m.y = 126;
		break;
	case 7:
		m.x = 357;m.y = 126;
		break;
	case 8:
		m.x = 556;m.y = 130;
		break;
	default:
		printf("非法參數,異常\n");
		break;
	}
	if (g_pOnMouse == NULL)
	{
		printf("貌似倉鼠沒有加載成功哎╮(￣▽￣\")╭,即將退出\n %s", SDL_GetError());
		return 0;
	}
	SDL_RenderCopy(g_pRenderer, g_pOnMouse, NULL, &m);
	SDL_RenderPresent(g_pRenderer);
	return 1;
}


void Analyzing_presence_hamster() {
	qw = 0;
	for (int i = 1;i < 9;i++) {
		if (weizhi[i] == 1) {
			on_mouse(i);//打印倉鼠的位置
			qw++;
		}
	}
	if (qw == 0) {
		mouse();
		so++;
	}
}

void hit_mouse(int x, int y) {
	if (x >= 220 && x <= 270 && y >= 145 && y <= 185) {
		weizhi[6] = 0;//位置7
	}
	else if (x >= 401 && x <= 401 + 50 && y >= 145 && y <= 185) {
		weizhi[7] = 0;//位置8
	}
	else if (x >= 607 && x <= 607 + 50 && y >= 145 && y <= 185) {
		weizhi[8] = 0;//位置9
	}
	else if (x >= 190 && x <= 190 + 50 && y >= 271 - 40 && y <= 271) {
		weizhi[3] = 0;//位置4
	}
	else if (x >= 411 && x <= 411 + 50 && y >= 270 - 40 && y <= 270) {
		weizhi[4] = 0;//位置5
	}
	else if (x >= 605 && x <= 605 + 50 && y >= 270 - 40 && y <= 270) {
		weizhi[5] = 0;//位置6
	}
	else if (x >= 181 && x <= 181 + 50 && y >= 366 - 40 && y <= 366) {
		weizhi[0] = 0;//位置1
	}
	else if (x >= 410 && x <= 410 + 50 && y >= 376 - 50 && y <= 376) {
		weizhi[1] = 0;//位置2
	}
	else if (x >= 630 && x <= 630 + 50 && y >= 376 - 50 && y <= 376) {
		weizhi[2] = 0;//位置3
	}
	else {
		//printf("点偏了\n");
	}
}